package com.zjy.software.day18;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamDemo {
    public static void main(String[] args) {
//        testEndStep();

        List<Integer> list = Arrays.asList(1,2,3, 4, 5, 6, 7, 8, 9, 10,2,1);
        list.stream().filter(a->a>6).forEach(System.out::println);

        //去重后的流
//        list.stream().distinct().forEach(System.out::println);

        //给对象排序,默认是调用对象的compareTo方法,因为要指定排序规则
//        list.stream().sorted().forEach(System.out::println);

        //limit(5)取前5个 截取前5个
//        list.stream().limit(5).forEach(System.out::println);
         //skip(5)跳过前5个
        list.stream().skip(5).forEach(System.out::println);

        //map是对流中的元素进行修改,并且再返回新的元素
        list.stream().map(a->a+10).forEach(System.out::println);

        list.stream().mapToInt(a->a+1).forEach(System.out::println);

        list.stream().collect(Collectors.toSet()).forEach(System.out::println);


//        list.stream().collect(Collectors.toMap(a->a,b->b+10)).forEach((k,v)->{
//            System.out.println("k = " + k);
//            System.out.println("v = " + v);
//        });


        List<String> strList=Arrays.asList("aaa","bbb","ccc","ddd","eee");




        
        list.stream().filter(a->a>3).sorted().distinct().map(a->a*20).skip(2).limit(3).forEach(System.out::println);

        float v = list.stream().collect(Collectors.summingInt(Integer::valueOf)).floatValue();
        System.out.println("v = " + v);

        String collect = strList.stream().collect(Collectors.joining(","));
        System.out.println("collect = " + collect);
    }

    private static void testEndStep() {
        List<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");

        //从集合中获取流,默认是是Stream
        Stream<String> stream = list.stream();

        stream.forEach(System.out::println);


        List<Integer> intList= Arrays.asList(8,6,2,3,4);
        Optional<Integer> min = intList.stream().min((o1, o2) -> {
            if (o1 > o2) {
                return 1;
            } else if (o1 < o2) {
                return -1;
            } else {
                return 0;
            }
        });

        Integer i = intList.stream().min(Integer::compareTo).get();
        System.out.println("intList.stream().max(Integer::compare).get() = " + intList.stream().max(Integer::compare).get());
        System.out.println("i = " + i);

        //判断当前流里面的每个元素都大于1,如果都大于1返回true,否则返回false
        System.out.println("intList.stream().allMatch(a->a>1) = " + intList.stream().allMatch(a -> a > 1));

        //判断当前流中是否有元素大于7,只要有一个,那么返回为true
        System.out.println("intList.stream().anyMatch((a)->{ return  a>7;}) = " + intList.stream().anyMatch((a) -> {
            return a > 7;
        }));

        intList.stream().map((a)->a*2).forEach(System.out::println);

        System.out.println("intList.stream().findFirst().get() = " + intList.stream().findFirst().get());

        //判断当前流中没有大于20的元素,返回true
        System.out.println("intList.stream().noneMatch((a)->(a>20)) = " + intList.stream().noneMatch((a) -> (a > 20)));


        System.out.println("min.get() = " + min.get());


//        System.out.println("stream.count() = " + stream.count());


        //演示把最终的流转换为集合
//        List<String> collect = stream.collect(Collectors.toList());

//        System.out.println("collect = " + collect);

//        Set<String> collect1 = stream.collect(Collectors.toSet());
//        for (String s : collect1) {
//            System.out.println("s = " + s);
//        }
    }
}
